#!/bin/bash

# start
echo "will install command:"
echo "wget"
echo "ifconfig"
echo "python3.11"
echo "django4.1"
echo "mysql5.7"
read -p "Do u want continue?(y/N):" input_val
if [ $input_val != "y" ]; then
    echo "exit" >> check_log.log
    exit 0
fi

# 修改源为阿里云源，需要的话取消掉下面的注释
#repoBackFile=/etc/yum.repos.d/CentOS-Base.repo.backup.$(cat /proc/sys/kernel/random/uuid)
#mv /etc/yum.repos.d/CentOS-Base.repo $repoBackFile
#curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
#yum clean all
#yum makecache
#yum update -y

yum install net-tools.x86_64 -y

yum install wget -y

cd /root
wget https://tools-ewaf.oss-cn-hangzhou.aliyuncs.com/2.%E8%BF%90%E8%A1%8C%E7%8E%AF%E5%A2%83/python/Python-3.11.0.tgz
tar -zxf Python-3.11.0.tgz
yum -y install gcc zlib zlib-devel libffi libffi-devel
yum -y install readline-devel
yum -y install epel-release
yum -y install openssl-devel openssl11 openssl11-devel
cd Python-3.11.0/
export CFLAGS=$(pkg-config --cflags openssl11)
export LDFLAGS=$(pkg-config --libs openssl11)
./configure --prefix=/usr/python --with-ssl=/usr/bin/openssl11
make
make install
ln -s /usr/python/bin/python3 /usr/bin/python3
ln -s /usr/python/bin/pip3 /usr/bin/pip3
cd ~/
mkdir .pip
cd .pip
touch pip.conf
echo "[global]" >> pip.conf
echo "index-url=http://mirrors.aliyun.com/pypi/simple/" >> pip.conf
echo "[install]" >> pip.conf
echo "trusted-host=mirrors.aliyun.com" >> pip.conf
cd ~/
python3 -m pip install --upgrade pip

echo "[error] command 'django' not found!"
echo "[start] install 'django'"
echo "install Django" >> check_log.log
pip3 install Django==4.1
ln /usr/python/bin/django-admin /usr/bin/django-admin

echo "[error] command 'mysql' not found!"
echo "[start] install 'mysql'"
wget http://repo.mysql.com//mysql57-community-release-el7-7.noarch.rpm
rpm -ivh mysql57-community-release-el7-7.noarch.rpm
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
yum install -y mysql-server
yum install -y mysql-devel
yum install -y mysql
systemctl enable mysqld
systemctl start mysqld
echo "install success!"

# 获取默认密码
mysqlLog=$(cat /var/log/mysqld.log | grep "root@localhost: .*$")
IFS=": "
read -ra strArr<<<"$mysqlLog"
maxNum=${#strArr}-1
DefaultMysqlPwd=${strArr[$maxNum]}

# 修改root密码
mysql --user="root" --password="Root@2023" --execute="ALTER USER USER() IDENTIFIED BY 'Root@2023';"


# 恢复原来的源文件，没有修改源的话不需要
#echo "reback repofile" >> check_log.log
#rm -rf /etc/yum.repos.d/CentOS-Base.repo
#mv $repoBackFile /etc/yum.repos.d/CentOS-Base.repo
#yum clean all
#yum makecache
#yum update

echo "=============================="
echo ""
echo "Install Env Success!"
echo ""
echo ""

pythonVer=$(python3 --version)
djangoVer=$(django-admin --version)
mysqlVer=$(mysql --version)
echo "python version: $pythonVer"
echo "django version: $djangoVer"
echo "mysql  version: $mysqlVer"

echo ""
echo "mysql root passwd: Root@2023"